var http = require('http'),
	https = require('https'),
	express = require('express'),
	session = require('express-session'),
	bodyParser = require('body-parser'),
	fs = require('fs'),
	path = require('path'),
	conf = JSON.parse(fs.readFileSync(path.join(__dirname, './') + './conf.json')),
	app = express(),
/*	sslOption = {
		key: fs.readFileSync(conf.sslKey),
		cert: fs.readFileSync(conf.sslPem)
	},*/
	reqFilter = require('./backend/frame/web/req-filter'),
	routers = require('./backend/router'),
	mongoUtils = require('./backend/frame/utils/mongo-utils');

//app.use(express.static(path.join(__dirname, './acctmrg-front')));
app.use(bodyParser.urlencoded({    
  extended: true
}));
app.use(bodyParser.json({
	extended: true
}));

// 按照上面的解释，设置 session 的可选参数
app.use(session({
	secret: '0OTRB58LNdGPTII1RfImvWOl5LFDuT5d4GGUgWQsselaEyZ0ys9C70ljiH7Htkv62RdoLEaEpdQVj150RBLnJHUKQo9LhirFwchOaRoYh4Su4U1Ah3Ry6tOLbPpsrVDw', // 建议使用 128 个字符的随机字符串
	cookie: { maxAge: 30 * 60 * 1000 }
}));
//注册过滤器
app.use(reqFilter.requestFilter);

// 创建服务端
http.createServer(app).listen(conf.port, function() {
	console.log('启动服务器完成,端口：'+conf.port);
});
/*if(conf.env === "pro"){
	https.createServer(sslOption,app).listen(conf.sslPort,function () {
		console.log('启动SSL服务器完成,端口号: '+conf.sslPort);
	});
}*/

app.set('trust proxy', 'loopback');

app.get('/',function(req,res){
	res.redirect(conf.welcome);
});

routers.init(app);